<template>
  <div class="pay">
    <div class="container">
      <xtx-bread>
        <xtx-bread-item to="/">首页</xtx-bread-item>
        <xtx-bread-item to="/cart">购物车</xtx-bread-item>
        <xtx-bread-item>支付订单</xtx-bread-item>
      </xtx-bread>
      <!-- 付款信息 -->
      <div class="pay-info" v-if="order">
        <span class="icon iconfont icon-queren2"></span>
        <div class="tip">
          <p>订单提交成功！请尽快完成支付</p>
          <p v-if="order.countdown > -1">
            支付还剩 <span>{{ timeText }}</span
            >，超时后将取消订单
          </p>
          <p v-else>订单已经超时</p>
        </div>
        <div class="amount">
          <span>应付总额：</span>
          <span>¥{{ order.payMoney }}</span>
        </div>
      </div>
      <!-- 付款方式 -->
      <div class="pay-type">
        <p class="head">请选择以下支付方式付款</p>
        <div class="item">
          <p>支付平台</p>
          <a
            @click="btnClick(1)"
            class="btn wx"
            href="javascript:;"
            :class="{ active: activeIndex === 1 }"
          ></a>
          <a
            @click="btnClick(2)"
            :class="{ active: activeIndex === 2 }"
            class="btn alipay"
            :href="payUrl"
            target="_blank"
          ></a>
        </div>
        <div class="item">
          <p>支付方式</p>
          <a
            @click="btnClickYinhang(1)"
            :class="{ active: activeIndexYinhang === 1 }"
            class="btn"
            href="javascript:;"
            >招商银行</a
          >
          <a
            @click="btnClickYinhang(2)"
            :class="{ active: activeIndexYinhang === 2 }"
            class="btn"
            href="javascript:;"
            >工商银行</a
          >
          <a
            @click="btnClickYinhang(3)"
            :class="{ active: activeIndexYinhang === 3 }"
            class="btn"
            href="javascript:;"
            >建设银行</a
          >
          <a
            @click="btnClickYinhang(4)"
            :class="{ active: activeIndexYinhang === 4 }"
            class="btn"
            href="javascript:;"
            >农业银行</a
          >
          <a
            @click="btnClickYinhang(5)"
            :class="{ active: activeIndexYinhang === 5 }"
            class="btn"
            href="javascript:;"
            >交通银行</a
          >
        </div>
      </div>
    </div>
    <xtx-dialog title="正在支付..." v-model="dialogVisable">
      <div class="pay-wait">
        <img src="@/assets/images/load.gif" alt="" />
        <div v-if="order">
          <p>如果支付成功：</p>
          <router-link :to="`/member/order/${order.id}`"
            >查看订单详情</router-link
          >
          <p>如果支付失败：</p>
          <router-link to="/">查看相关疑问</router-link>
        </div>
      </div>
    </xtx-dialog>
  </div>
</template>
<script>
import { ref } from 'vue'
import { findOrderDetail } from '@/api/order'
import { useRoute } from 'vue-router'
import { usePayTime } from '@/hooks'
import { baseURL } from '@/utils/request'
export default {
  name: 'Pay',
  setup() {
    // 支付平台
    const activeIndex = ref(1)
    const btnClick = (index) => {
      activeIndex.value = index
      dialogVisable.value = true
    }
    // 支付方式
    const activeIndexYinhang = ref(1)
    const btnClickYinhang = (index) => {
      activeIndexYinhang.value = index
    }

    // 根据地址栏ID发请求获取订单数据
    const route = useRoute()
    const order = ref(null)
    // 查询订单
    findOrderDetail(route.query.orderId).then((data) => {
      // 设置订单
      order.value = data.result
      // 后端提供countdown倒计时秒数
      if (data.result.countdown > -1) {
        start(data.result.countdown)
      }
    })
    // 支付地址
    // const payUrl = '后台服务基准地址+支付页面地址+订单ID+回跳地址'
    const redirect = 'http://www.corho.com:8080/#/pay/callback'
    const payUrl = `${baseURL}pay/aliPay?orderId=${route.query.orderId}&redirect=${redirect}`

    // 支付提示
    const dialogVisable = ref(false)
    const { start, timeText } = usePayTime()
    return {
      activeIndex,
      btnClick,
      btnClickYinhang,
      activeIndexYinhang,
      order,
      timeText,
      payUrl,
      dialogVisable
    }
  }
}
</script>
<style scoped lang="less">
.pay-info {
  background-color: #fff;
  padding: 0 80px;
  display: flex;
  align-items: center;
  height: 240px;
  .icon {
    color: #1dc779;
    font-size: 80px;
  }
  .tip {
    padding-left: 10px;
    flex: 1;
    p {
      &:first-child {
        font-size: 20px;
        margin-bottom: 5px;
      }
      &:last-child {
        color: #999;
        font-size: 16px;
      }
    }
  }
  .amount {
    span {
      &:first-child {
        color: #999;
        font-size: 16px;
      }
      &:last-child {
        font-size: 20px;
        color: @priceColor;
      }
    }
  }
}
.pay-type {
  background-color: #fff;
  margin-top: 20px;
  padding-bottom: 70px;
  margin-bottom: 20px;
  p {
    height: 70px;
    line-height: 70px;
    font-size: 16px;
    padding-left: 30px;
    &.head {
      border-bottom: 1px solid #f5f5f5;
    }
  }
  .btn {
    width: 150px;
    height: 50px;
    border: 1px solid #e4e4e4;
    text-align: center;
    line-height: 50px;
    margin-left: 30px;
    color: #666666;
    display: inline-block;
    &.active,
    &:hover {
      border-color: @color;
    }
    &.alipay {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png)
        no-repeat center / contain;
    }
    &.wx {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg)
        no-repeat center / contain;
    }
  }
}
.pay-wait {
  display: flex;
  justify-content: space-around;
  p {
    margin-top: 30px;
    font-size: 14px;
  }
  a {
    color: @color;
  }
}
</style>
